package com.chinawlx.wlxteacher.widget.record;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import com.chinawlx.wlxteacher.R;

public class AudioRecordDialog {
    private Dialog dialog;
    private ImageView imageRecord;

    private Context context;
    private AnimationDrawable mAnim;

    public AudioRecordDialog(Context context) {
        this.context = context;
    }

    public void showDialog() {
        dialog = new Dialog(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.audio_dialog, null);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);

        imageRecord = (ImageView) dialog.findViewById(R.id.ivRecord);
//        imageRecord.setBackgroundResource(R.drawable.anim_audio_recording);
//        mAnim = (AnimationDrawable) imageRecord.getBackground();
//        mAnim.start();
        dialog.show();
        stateRecording();
    }

    public void stateRecording() {
        if (dialog != null && dialog.isShowing() && imageRecord != null) {
            imageRecord.setImageResource(0);//清空图片背景

            imageRecord.setBackgroundResource(R.drawable.anim_audio_recording);
            mAnim = (AnimationDrawable) imageRecord.getBackground();
            mAnim.start();
        }
    }

    public void stateWantCancel() {
        if (dialog != null && dialog.isShowing() && imageRecord != null) {
            imageRecord.setBackgroundResource(0);//清空背景资源,动画

            imageRecord.setImageResource(R.drawable.fingersslipcancelsending);
        }
    }

    public void stateLengthShort() {
        if (dialog != null && dialog.isShowing()) {
            Toast.makeText(context, "录制时间太短", Toast.LENGTH_SHORT).show();
        }
    }

    public void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
